import { clsx, type ClassValue } from 'clsx'
import { twMerge } from 'tailwind-merge'

export const JWT_TOKEN_KEY = 'jwtToken';

export function cn(...inputs: ClassValue[]) {
  return twMerge(clsx(inputs))
}

export function fetchData(url: string, options?: RequestInit) {
  return fetch(url, options).then(res => res.json()).then(res => {
    if (res?.code !== 1) throw res;
    return res.data;
  });
}

export function fetchWithJwtToken(url: string, options?: RequestInit) {
  return fetchData(url, {
    ...options,
    headers: {
      Authorization: localStorage.getItem(JWT_TOKEN_KEY) || '',
      ...options?.headers,
    },
  });
}
